package Day022_XML_Dom4j.ZuoYe;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.lang.reflect.Constructor;
import java.util.List;

/**
 * @author zhangyan
 * @date 2020/5/16 16:58
 */
public class Test2 {
    public static void main(String[] args) throws Exception {

        //获取解析器
        SAXReader sr = new SAXReader();

        //解析文件 获取Document对象
        Document document = sr.read("Demo02\\src\\Day022_XML_Dom4j\\ZuoYe\\Person.xml");

        //获取根元素
        Element root = document.getRootElement();

        //获取根元素下所有元素
        List<Element> list = root.elements();

        //遍历集合
        for (Element e : list) {
            Node node1 = e.selectSingleNode("./name");
            String s1 = node1.getText();
            Node node2 = e.selectSingleNode("./sex");
            String s2 = node2.getText();
            Node node3 = e.selectSingleNode("./age");
            String s3 = node3.getText();

            //把获取的年龄的文本转换成int类型
            int i = Integer.parseInt(s3);

            //通过类名获取Class对象
            Class<Person> c = Person.class;

            //获取非public修饰构造方法
            Constructor<Person> cons = c.getDeclaredConstructor(String.class, String.class, int.class);

            //设置暴力反射
            cons.setAccessible(true);
            //执行获取的构造方法
            Person person = cons.newInstance(s1, s2, i);
            System.out.println(person);
        }
    }
}

